package vip.web3.common.utils;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;

import java.io.*;
import java.util.LinkedHashMap;

/**
 * aut-horhuwei
 * @desc 操作yml的工具类
 * @date 2019-06-06 20:03
 */
public class YmlUtils {

    private final static DumperOptions OPTIONS = new DumperOptions();
    private final static String InitYmlUrl ="application.yml";
    public static String InitProfileUrl;
    private static LinkedHashMap<String,Object> InitLinkedHashMap;
    private static LinkedHashMap<String,Object> profileLinkedHashMap;

    static {
        //将默认读取的方式设置为块状读取
        OPTIONS.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
        Yaml yaml = new Yaml(OPTIONS);
        try {
            InputStreamReader InitInputStreamReader = new InputStreamReader(YmlUtils.class.getClassLoader().getResourceAsStream(InitYmlUrl),"UTF-8");
            //载入文件
            InitLinkedHashMap = yaml.load(InitInputStreamReader);
            String profile =getInitFromYml("spring.profiles.active").toString();
            InputStreamReader profileInputStreamReader = new InputStreamReader(YmlUtils.class.getClassLoader().getResourceAsStream("application-"+profile+".yml"),"UTF-8");
            profileLinkedHashMap = yaml.load(profileInputStreamReader);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 在目标文件中添加新的配置信息
     *
     * @param dest  需要添加信息的目标yml文件
     * @param key   添加的key值
     * @param value 添加的对象(如key下方还有链接则添加LinkedHashMap)
     * aut-horRelic
     * @title addIntoYml
     * @date 2019/1/29 20:52
     */
    public static void addIntoYml(File dest, String key, Object value) throws IOException {
        Yaml yaml = new Yaml(OPTIONS);
        //载入当前yml文件
        LinkedHashMap<String, Object> dataMap = yaml.load(new FileReader(dest));
        //如果yml内容为空,则会引发空指针异常,此处进行判断
        if (null == dataMap) {
            dataMap = new LinkedHashMap<>();
        }
        dataMap.put(key, value);
        //将数据重新写回文件
        yaml.dump(dataMap, new FileWriter(dest));
    }

    /**
     * 从公共配置中读取出所指定key的值 application.yml
     * @param key    需要获取信息的key值
     * @return java.lang.Object
     * aut-horhuwei
     * @title getInitFromYml
     * @date 2019/1/29 20:56
     */
    public static Object getInitFromYml(String key) throws IOException {
        //获取当前key下的值(如果存在多个节点,则value可能为map,自行判断)
        return getYamlKey(InitLinkedHashMap,key);
    }
    /**
     * 从目标yml文件中读取出所指定key的值
     * @param key    需要获取信息的key值
     * @return java.lang.Object
     * aut-horhuwei
     * @title getFromYml
     * @date 2019/6/6 20:56
     */
    public static Object getFromYml(String key) throws IOException {
        return getYamlKey(profileLinkedHashMap,key);
    }
    public static  Object  getYamlKey(LinkedHashMap<String, Object> linkedHashMap,String key){
         String keys[] = key.split("\\.");
        LinkedHashMap<String, Object> linkedHashMap1=linkedHashMap;
         for(String str:keys){
             Object object = linkedHashMap1.get(str);
           if(object instanceof LinkedHashMap){
               linkedHashMap1 =(LinkedHashMap<String, Object>) object;
           }else {
               return object;
           }
         }
         return linkedHashMap1;
    }
    public static void main(String[] args) {
                try{
                System.out.println(getFromYml("props.qRcodeUrl"));
            }catch (Exception e){

            }

    }
}